//
//  ViewController.m
//  DXC
//
//  Created by TOPTEAM on 2017/5/8.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "ViewController.h"
#import "Common.h"
#import "BaseCell.h"
#import "Time_Consuming_VC.h"
#import "NSThread_VC.h"
#import "ThreadState_VC.h"
#import "Inter_thread_communication_VC.h"
#import "Thread_safety_VC.h"
#import "GCD_Basis_VC.h"
#import "Operation_BaseUse_VC.h"
#import "NSOperationQueue_VC.h"
#import "Operation_OtherUse_VC.h"
#import "GCD_Queue_Group_VC.h"
@interface ViewController ()

<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)ExceptionView *exceptionView;
@property(nonatomic, strong)NSMutableArray*marr_Section;
@property(nonatomic, strong)NSMutableArray*marr_Data;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    [self  setProperty];
    [self  createUI];
    [self  check_NetWork];

}


#pragma mark==================setProperty======================
-(void)setProperty{

    _marr_Data=[[NSMutableArray alloc]init];
    
    self.view .backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"DXC";
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    _marr_Section=[[NSMutableArray alloc]init];
    
}

#pragma mark==================createUI======================
-(void)createUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
    
}
#pragma mark==================checkNet======================
-(void)check_NetWork{
    RequestReachabilityStatus status = [RequestManager reachabilityStatus];
    switch (status) {
        case RequestReachabilityStatusReachableViaWiFi:
        case RequestReachabilityStatusReachableViaWWAN:
        case RequestReachabilityStatusUnknown:
        {
          
            
            [self Create_Marr_Section];
            
        }
            break;
        default: {
            [HUD dismiss];
            
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoNetwork];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoNetwork handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self check_NetWork];
            }];
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self.view];
        }
            break;
    }
}




-(void)Create_Marr_Section{
    
    [_marr_Data addObject:@"耗时操作"];
    [_marr_Data addObject:@"pthread"];
    [_marr_Data addObject:@"NSThread"];
    [_marr_Data addObject:@"线程状态"];
    [_marr_Data addObject:@"线程间通信"];
    [_marr_Data addObject:@"线程安全"];
    [_marr_Data addObject:@"GCD基本使用"];
    [_marr_Data addObject:@"GCD常用函数"];
    [_marr_Data addObject:@"NSOperation基本使用"];
    [_marr_Data addObject:@"NSOperationQueue"];
    [_marr_Data addObject:@"NSOperation操作依赖和监听"];
    [_marr_Data addObject:@"多图片下载"];
    [_marr_Data addObject:@"GCD队列组"];
    
    [_marr_Section removeAllObjects];
    
//    NSMutableArray * marr_Cells=[[NSMutableArray alloc]init];
    
    for(int i=0 ;i<_marr_Data.count; i++){
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([BaseCell class]);
        model.className=NSStringFromClass([BaseCell class]);
        model.height = [BaseCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        
        /*
         * 传递参数
         */
        model.userInfo = [_marr_Data objectAtIndex:i];
        
//        [marr_Cells addObject:model];
        SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:@[model]];
        model0.headerhHeight=0.0001;
        model0.footerHeight=10;
        [_marr_Section addObject:model0];
    }
//    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr_Cells];
//    model0.headerhHeight=0.0001;
//    model0.footerHeight=10;
//    [_marr_Section addObject:model0];
    
    [_tableView reloadData];
    
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([BaseCell class])]) {
        BaseCell *ex_cell=(BaseCell *)cell;
        
        [ex_cell relayOutUIWithModel:cm.userInfo];
        
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];

    if ([cm.userInfo isEqualToString:@"耗时操作"]) {
        
        
        
        //获取storyboard: 通过bundle根据storyboard的名字来获取我们的storyboard,
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Time_Consuming_VC" bundle:[NSBundle mainBundle]];
        //由storyboard根据myView的storyBoardID来获取我们要切换的视图
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"Time_Consuming_VC"];
        //由navigationController推向我们要推向的view
        [self.navigationController pushViewController:myView animated:YES];
        

    }else if([cm.userInfo isEqualToString:@"pthread"]){
        
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Pthread_VC" bundle:[NSBundle mainBundle]];
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"Pthread_VC"];
        [self.navigationController pushViewController:myView animated:YES];

    }else if([cm.userInfo isEqualToString:@"NSThread"]){
        
        NSThread_VC * VC =[[NSThread_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];

    }else if([cm.userInfo isEqualToString:@"线程状态"]){
        
        ThreadState_VC * VC =[[ThreadState_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"线程间通信"]){
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Inter_thread_communication_VC" bundle:[NSBundle mainBundle]];
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"Inter_thread_communication_VC"];
        [self.navigationController pushViewController:myView animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"线程安全"]){
        
        Thread_safety_VC * VC =[[Thread_safety_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"GCD基本使用"]){
        
        GCD_Basis_VC * VC =[[GCD_Basis_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"GCD常用函数"]){
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"GCD_Function_VC" bundle:[NSBundle mainBundle]];
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"GCD_Function_VC"];
        [self.navigationController pushViewController:myView animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"NSOperation基本使用"]){
        
        Operation_BaseUse_VC * VC =[[Operation_BaseUse_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if([cm.userInfo isEqualToString:@"NSOperation其他用法"]){
        
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Operation_OtherUse_VC" bundle:[NSBundle mainBundle]];
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"Operation_OtherUse_VC"];
        [self.navigationController pushViewController:myView animated:YES];

        
    }else if([cm.userInfo isEqualToString:@"NSOperationQueue"]){
        
        NSOperationQueue_VC * VC =[[NSOperationQueue_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
        
    }else if([cm.userInfo isEqualToString:@"NSOperation操作依赖和监听"]){
        
        
        
    }else if([cm.userInfo isEqualToString:@"多图片下载"]){
        
        
        
    }else if([cm.userInfo isEqualToString:@"GCD队列组"]){
        
        GCD_Queue_Group_VC * VC = [[GCD_Queue_Group_VC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }

   
   
}


@end
